package com.wenyu.wenyuyuedu.adapter.circleAdapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.LoginActivity;
import com.wenyu.wenyuyuedu.activity.personage.Help_FeedBack_Activity;
import com.wenyu.wenyuyuedu.bean.circle.JoinCircleBean;
import com.wenyu.wenyuyuedu.bean.circle.MyCircleFind;
import com.wenyu.wenyuyuedu.bean.personage.JudgeLoginBean;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.circle.JoinCircleServer;
import com.wenyu.wenyuyuedu.server.personage.JudgeLoginServer;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Administrator on 2017/3/7.
 * 首页发现的adapter
 */

public class MyCircleFindAdapter extends BaseAdapter {
    private List<MyCircleFind.InfoBean> info = new ArrayList<>();
    private Activity context;
    private View view;
    private MyFindHolder holder;
    private String token;

    private String login_uid;

    public MyCircleFindAdapter(Activity context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return info.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        holder = null;
        if (view == null) {
            holder = new MyFindHolder();
            view = View.inflate(context, R.layout.mycircle_find_item, null);
            holder.mycircle_find_content = (TextView) view.findViewById(R.id.mycircle_find_content);
            holder.mycircle_find_name = (TextView) view.findViewById(R.id.mycircle_find_name);
            //holder.mycircle_find_comein = (TextView) view.findViewById(R.id.mycircle_find_comein);
            holder.mycircle_find_join = (TextView) view.findViewById(R.id.mycircle_find_join);
            holder.tv_mycircle_find_type = (TextView) view.findViewById(R.id.tv_mycircle_find_type);
            holder.mycircle_find_member = (TextView) view.findViewById(R.id.mycircle_find_member);
            holder.mycircle_find_photo = (ImageView) view.findViewById(R.id.mycircle_find_photo);
            view.setTag(holder);
        } else {
            holder = (MyFindHolder) view.getTag();
        }

        if (info.size() != 0) {
            final MyCircleFind.InfoBean infoBean = info.get(i);
            /*圈子的介绍*/
            holder.mycircle_find_content.setText(infoBean.getContent());
            /*圈子的图片*/
            Glide.with(context)
                    .load(infoBean.getCover())
                    .error(R.drawable.wenyulogo)
                    .into(holder.mycircle_find_photo);
            /*圈子的名字*/
            holder.mycircle_find_name.setText(infoBean.getTitle());
            /*圈子所属类目名字*/
            holder.tv_mycircle_find_type.setText(infoBean.getCircleType());
            /*圈子的人数*/
            String collect_count = infoBean.getCollect_count();
            int collectCount = Integer.parseInt(collect_count);
            if(collectCount < 100){
                holder.mycircle_find_member.setText(infoBean.getCollect_count() + "位成员");
            }else if(100 <= collectCount && collectCount < 200){
                holder.mycircle_find_member.setText(infoBean.getCollect_count() + "+位成员");
            }else if(200 <= collectCount && collectCount < 300){
                holder.mycircle_find_member.setText(infoBean.getCollect_count() + "+位成员");
            }else if(300 <= collectCount && collectCount < 400){
                holder.mycircle_find_member.setText(infoBean.getCollect_count() + "+位成员");
            }else if(400 <= collectCount && collectCount < 500){
                holder.mycircle_find_member.setText(infoBean.getCollect_count() + "+位成员");
            }else if(500 <= collectCount && collectCount < 600){
                holder.mycircle_find_member.setText(infoBean.getCollect_count() + "+位成员");
            }else if(600 <= collectCount && collectCount < 700){
                holder.mycircle_find_member.setText(infoBean.getCollect_count() + "+位成员");
            }else if(700 <= collectCount && collectCount < 800){
                holder.mycircle_find_member.setText(infoBean.getCollect_count() + "+位成员");
            }else if(800 <= collectCount && collectCount < 900){
                holder.mycircle_find_member.setText(infoBean.getCollect_count() + "+位成员");
            }else if(900 <= collectCount && collectCount < 1000){
                holder.mycircle_find_member.setText(infoBean.getCollect_count() + "+位成员");
            }else if(1000 <= collectCount && collectCount < 2000){
                holder.mycircle_find_member.setText(infoBean.getCollect_count() + "+位成员");
            }else if(2000 <= collectCount){
                holder.mycircle_find_member.setText(infoBean.getCollect_count() + "+位成员");
            }
               /*判断显示的是加入书圈还是进入书圈*/
            if (info.get(i).isChecked()) {
                holder.mycircle_find_join.setBackgroundResource(R.drawable.mycircle_find_comein);
                holder.mycircle_find_join.setText("进入书圈");
                holder.mycircle_find_join.setTextColor(context.getResources().getColor(R.color.black));
            } else {
                holder.mycircle_find_join.setBackgroundResource(R.drawable.mycircle_find_join_shape);
                holder.mycircle_find_join.setText("加入书圈");
                holder.mycircle_find_join.setTextColor(context.getResources().getColor(R.color.white));
            }
            holder.mycircle_find_join.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                     /*判断用户是否登陆*/
                    JudgeLoginServer judgeLoginServer = ApiWrapper.getInstance().create(JudgeLoginServer.class);
                    Call<JudgeLoginBean> judgeLogin = judgeLoginServer.getJudgeLogin(token);
                    judgeLogin.enqueue(new Callback<JudgeLoginBean>() {
                        @Override
                        public void onResponse(Call<JudgeLoginBean> call, Response<JudgeLoginBean> response) {
                            JudgeLoginBean body = response.body();
                            if (body != null) {
                                int code = body.getCode();
                                if (code == 1) {
                                    if (info.get(i).isChecked()) {
                                        Intent intent = new Intent(context, Help_FeedBack_Activity.class);
                                        intent.putExtra("creatorName", info.get(i).getCretorName());
                                        intent.putExtra("circleId", info.get(i).getId());
                                        intent.putExtra("circleName", info.get(i).getTitle());
                                        intent.putExtra("circlecover", info.get(i).getCover());
                                        intent.putExtra("login_find_uid",login_uid);
                                        context.startActivity(intent);
                                        return;
                                    } else {
                                        JoinCircleServer joinCircleServer = ApiWrapper.getInstance().create(JoinCircleServer.class);
                                        Call<JoinCircleBean> joinBean = joinCircleServer.getJoinBean(token, Integer.parseInt(info.get(i).getId()), 1, 1);
                                        joinBean.enqueue(new Callback<JoinCircleBean>() {
                                            @Override
                                            public void onResponse(Call<JoinCircleBean> call, Response<JoinCircleBean> response) {
                                                JoinCircleBean body = response.body();
                                                if (body != null) {
                                                    int code = body.getCode();
                                                    if (code == 1) {
                                                        MyToast.showToast(context, "加入圈子成功", Toast.LENGTH_SHORT);
                                                    } else if (code == 0) {
                                                        MyToast.showToast(context, "请登陆", Toast.LENGTH_SHORT);
                                                    } else if (code == 2500) {
                                                        MyToast.showToast(context, "需要登陆后才可以操作", Toast.LENGTH_SHORT);
                                                    } else if (code == 2501) {
                                                        MyToast.showToast(context, "不存在该书圈", Toast.LENGTH_SHORT);
                                                    } else if (code == 2502) {
                                                        MyToast.showToast(context, "您已经收藏过了！", Toast.LENGTH_SHORT);
                                                    } else if (code == 2506) {
                                                        MyToast.showToast(context, "抱歉您的账户被暂时限制了，无法进行此操作", Toast.LENGTH_SHORT);
                                                    } else if (code == 2507) {
                                                        MyToast.showToast(context, "系统繁忙，请您稍后再试！", Toast.LENGTH_SHORT);
                                                    }
                                                }
                                            }

                                            @Override
                                            public void onFailure(Call<JoinCircleBean> call, Throwable t) {
                                                MyToast.showToast(context, "系统繁忙，请您稍后再试！", Toast.LENGTH_SHORT);
                                            }
                                        });
                                        info.get(i).setChecked(true);
                                    }
                                    notifyDataSetChanged();
                                } else if (code == 0) {
                                    showLoginDoalog();
                                } else if (code == 5000) {
                                    showLoginDoalog();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<JudgeLoginBean> call, Throwable t) {
                            MyToast.showToast(context, "系统繁忙，请您稍后再试！", Toast.LENGTH_SHORT);
                        }
                    });

                }
            });

        }
        return view;
    }

    class MyFindHolder {
        TextView mycircle_find_content, mycircle_find_name, mycircle_find_join, tv_mycircle_find_type;
        TextView mycircle_find_member;
        ImageView mycircle_find_photo;
    }

    public void AddAll(List<MyCircleFind.InfoBean> info, String token,String login_uid) {
        if (info != null) {
            this.info = info;
            this.token = token;
            this.login_uid = login_uid;
            notifyDataSetChanged();
        }
    }

    android.app.AlertDialog ad;
    private TextView quxiao;
    private TextView queding;

    /*显示登陆的dialog*/
    private void showLoginDoalog() {

        ad = new android.app.AlertDialog.Builder(context).create();
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.dailog_login);
        quxiao = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        queding = (TextView) window.findViewById(R.id.dialog_login_queding);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbSharedUtil.putString(context, SharedConstants.USER_TOKEN, "");
                Intent intent = new Intent();
                intent.setClass(context, LoginActivity.class);
                context.startActivity(intent);
                context.finish();
                ad.dismiss();
            }
        });
    }
}
